package com.ruoyi.zyt.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.zyt.domain.Field;
import com.ruoyi.zyt.domain.User;
import com.ruoyi.zyt.dto.CollegeDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zyt.domain.Colleges;
import com.ruoyi.zyt.service.ICollegesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 院校数据Controller
 *
 * @author zyt
 * @date 2023-11-04
 */
@RestController
@RequestMapping("/zyt/colleges")
public class CollegesController extends BaseController {
    @Autowired
    private ICollegesService collegesService;

    @GetMapping("/getCol")
    public AjaxResult getCol(CollegeDTO collegeDTO) {
        if (collegeDTO == null) {
            return error("考生分数不能为空！");
        }
        Integer num = Math.toIntExact(collegeDTO.getPlace());
        Colleges colleges2 = new Colleges();
        BeanUtils.copyProperties(collegeDTO, colleges2);
        List<Colleges> collegesList = collegesService.selectCollegesList(colleges2);
        String mode = collegeDTO.getMode();
        if ("all".equals(mode)) {
            collegesList = collegesList.stream().sorted((a, b) -> (int) (a.getNum2023() - b.getNum2023())
            ).collect(Collectors.toList());
        } else if ("getImpact".equals(mode)) {
            collegesList = collegesList.stream().filter(colleges -> colleges.getPlace() <= num && colleges.getPlace() > num-200).collect(Collectors.toList());
        } else if ("getSteady".equals(mode)) {
            collegesList = collegesList.stream().filter(colleges -> colleges.getPlace() <= num + 200 && colleges.getPlace() > num).collect(Collectors.toList());
        } else {
            collegesList = collegesList.stream().filter(colleges -> colleges.getPlace() <= num +700 && colleges.getPlace() >= num +200).collect(Collectors.toList());
        }
        return success(collegesList);
    }

    /**
     * 获取冲击的高校列表
     *
     * @return
     */
    @GetMapping("/getImpact")
    public AjaxResult getImpact(CollegeDTO collegeDTO) {
        if (collegeDTO == null) {
            return error("考生分数不能为空！");
        }
        Integer num = Math.toIntExact(collegeDTO.getPlace());
        Colleges colleges2 = new Colleges();
        BeanUtils.copyProperties(collegeDTO, colleges2);
        List<Colleges> collegesList = collegesService.selectCollegesList(colleges2);
        List<Colleges> impact = collegesList.stream().filter(colleges -> colleges.getPlace() <= num && colleges.getPlace() > num-200).collect(Collectors.toList());
        return success(impact);
    }

    /**
     * 获取稳妥的高校列表
     *
     * @return
     */
    @GetMapping("/getSteady")
    public AjaxResult getCollageByNum(CollegeDTO collegeDTO) {
        if (collegeDTO == null) {
            return error("考生分数不能为空！");
        }
        Integer num = Math.toIntExact(collegeDTO.getPlace());
        Colleges colleges2 = new Colleges();
        BeanUtils.copyProperties(collegeDTO, colleges2);
        List<Colleges> collegesList = collegesService.selectCollegesList(colleges2);
        List<Colleges> steady = collegesList.stream().filter(colleges -> colleges.getPlace() <= num + 200 && colleges.getPlace() > num).collect(Collectors.toList());
        return success(steady);
    }

    /**
     * 获取保底的高校列表
     *
     * @return
     */
    @GetMapping("/getDefend")
    public AjaxResult getDefend(CollegeDTO collegeDTO) {
        if (collegeDTO == null) {
            return error("考生分数不能为空！");
        }
        Integer num = Math.toIntExact(collegeDTO.getPlace());
        Colleges colleges2 = new Colleges();
        BeanUtils.copyProperties(collegeDTO, colleges2);
        List<Colleges> collegesList = collegesService.selectCollegesList(colleges2);
        List<Colleges> defend = collegesList.stream().filter(colleges -> colleges.getPlace() <= num +700 && colleges.getPlace() >= num +200).collect(Collectors.toList());
        return success(defend);
    }

    /**
     * 获取开设专业的高校列表
     *
     * @return
     */
    @GetMapping("/getCollages")
    public AjaxResult getCollageByField(Field field) {
        Colleges colleges = new Colleges();
        colleges.setFields(field.getName());
        List<Colleges> collegesList = collegesService.selectCollegesList(colleges);
        return success(collegesList);
    }

    /**
     * 获取推荐的高校列表
     *
     * @return
     */
    @GetMapping("/getSuggest")
    public AjaxResult getCollageByField(Colleges colleges) {
        if (colleges.getPlace() != null) {
            colleges.setNum2021(colleges.getPlace() - 200);
            colleges.setNum2022(colleges.getPlace() + 700);
        }
        List<Colleges> list = collegesService.selectSuggestCollegesList(colleges).stream().sorted(new Comparator<Colleges>() {
            @Override
            public int compare(Colleges o1, Colleges o2) {
                return (int) (o1.getPlace() - o2.getPlace());
            }
        }).collect(Collectors.toList());
        return success(list);
    }

    /**
     * 查询院校数据列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Colleges colleges) {
        startPage();
        List<Colleges> list = collegesService.selectCollegesList(colleges);
        return getDataTable(list);
    }

    /**
     * 导出院校数据列表
     */
    @Log(title = "院校数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Colleges colleges) {
        List<Colleges> list = collegesService.selectCollegesList(colleges);
        ExcelUtil<Colleges> util = new ExcelUtil<Colleges>(Colleges.class);
        util.exportExcel(response, list, "院校数据数据");
    }

    /**
     * 获取院校数据详细信息
     */
    @GetMapping(value = "/{collegesId}")
    public AjaxResult getInfo(@PathVariable("collegesId") Long collegesId) {
        return success(collegesService.selectCollegesByCollegesId(collegesId));
    }

    /**
     * 新增院校数据
     */
    @Log(title = "院校数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Colleges colleges) {
        return toAjax(collegesService.insertColleges(colleges));
    }

    /**
     * 修改院校数据
     */
    @Log(title = "院校数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Colleges colleges) {
        return toAjax(collegesService.updateColleges(colleges));
    }

    /**
     * 删除院校数据
     */
    @Log(title = "院校数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{collegesIds}")
    public AjaxResult remove(@PathVariable Long[] collegesIds) {
        return toAjax(collegesService.deleteCollegesByCollegesIds(collegesIds));
    }
}
